package com.metadata_system.api_server.system_server;


import com.alibaba.fastjson.JSON;
import com.metadata_system.api.annotations.CheckTokenUser;
import com.metadata_system.common.db.entity.MetadataEdge;
import com.metadata_system.common.dto.ApiResult;
import com.metadata_system.common.http.request_dto.LoginUserRequest;
import com.metadata_system.system.request.MetadataEdgeListParams;
import com.metadata_system.system.service.MetadataEdgeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/api/system-server/v001/MetadataEdge")
public class MetadataEdgeController {
    @Autowired
    private MetadataEdgeService metadataEdgeService;

    @Autowired
    private LoginUserRequest loginUserRequest;

    @RequestMapping("getList")
    @CheckTokenUser
    public ApiResult getList(
            @RequestParam(value = "name", required = false) String name,
            @RequestParam(value = "metadataViewId", required = false) Integer metadataViewId,
            @RequestParam(value = "metadataId", required = false) Integer metadataId,
            @RequestParam(value = "page", required = false) Integer page,
            @RequestParam(value = "pageSize", required = false) Integer pageSize,
            @RequestParam(value = "sortProp", required = false) String sortProp,
            @RequestParam(value = "sortOrder", required = false) String sortOrder
    ) {
        return metadataEdgeService.getList(MetadataEdgeListParams.builder()
                .name(name)
                .metadataViewId(metadataViewId)
                .metadataId(metadataId)
                .page(page)
                .pageSize(pageSize)
                .sortProp(sortProp)
                .sortOrder(sortOrder)
                .build());
    }


    @RequestMapping("create")
    @CheckTokenUser
    @Transactional
    public ApiResult create(
            @RequestParam(value = "name", required = false) String name,
            @RequestParam(value = "type", required = false) Integer type,
            @RequestParam(value = "source", required = false) Integer source,
            @RequestParam(value = "target", required = false) Integer target,
            @RequestParam(value = "properties", required = false, defaultValue = "[]") String properties,
            @RequestParam(value = "sourceHandle", required = false) String sourceHandle,
            @RequestParam(value = "targetHandle", required = false) String targetHandle,
            @RequestParam(value = "metadataId", required = false) Integer metadataId,
            @RequestParam(value = "metadataViewId", required = false) Integer metadataViewId
    ) {
        ApiResult apiResult = metadataEdgeService.create(MetadataEdge.builder()
                .name(name)
                .type(type)
                .source(source)
                .target(target)
                .sourceHandle(sourceHandle)
                .targetHandle(targetHandle)
                .metadataId(metadataId)
                .metadataViewId(metadataViewId)
                .properties(properties)
                .build(),loginUserRequest.getTokenUser().decodeID());
        if (apiResult.getCode() != 0) {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        }
        return apiResult;
    }

    @RequestMapping("update")
    @CheckTokenUser
    @Transactional
    public ApiResult update(
            @RequestParam(value = "id", required = false) Integer id,
            @RequestParam(value = "name", required = false) String name,
            @RequestParam(value = "type", required = false) Integer type,
            @RequestParam(value = "source", required = false) Integer source,
            @RequestParam(value = "target", required = false) Integer target,
            @RequestParam(value = "sourceHandle", required = false) String sourceHandle,
            @RequestParam(value = "targetHandle", required = false) String targetHandle,
            @RequestParam(value = "properties", required = false) String properties
    ) {
        ApiResult apiResult = metadataEdgeService.update(MetadataEdge.builder()
                .id(id)
                .name(name)
                .type(type)
                .source(source)
                .target(target)
                .sourceHandle(sourceHandle)
                .targetHandle(targetHandle)
                .properties(properties)
                .build(),loginUserRequest.getTokenUser().decodeID());
        if (apiResult.getCode() != 0) {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        }
        return apiResult;
    }


    @RequestMapping("delete")
    @CheckTokenUser
    @Transactional
    public ApiResult delete(
            @RequestParam(value = "id", required = false) Integer id,
            @RequestParam(value = "metadataViewId", required = false) Integer metadataViewId
    ) {
        ApiResult apiResult = metadataEdgeService.delete(id, metadataViewId, loginUserRequest.getTokenUser().decodeID());
        if (apiResult.getCode() != 0) {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        }
        return apiResult;
    }

    @RequestMapping("deleteMore")
    @CheckTokenUser
    @Transactional
    public ApiResult deleteMore(
            @RequestParam(value = "ids", required = false) String ids,
            @RequestParam(value = "metadataViewId", required = false) Integer metadataViewId
    ) {
        List<Integer> idList = new ArrayList<>();
        try {
            idList = JSON.parseArray(ids, Integer.class);
            for (Integer id : idList) {
                if (id <= 0) {
                    return ApiResult.error("删除关系-参数错误 1");
                }
            }
        } catch (Exception e) {
            return ApiResult.error("删除关系-参数错误 2");
        }
        ApiResult apiResult = metadataEdgeService.deleteAll(idList, metadataViewId, loginUserRequest.getTokenUser().decodeID());
        if (apiResult.getCode() != 0) {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        }
        return apiResult;
    }


}
